package com.pilipili.zeus.eduService.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pilipili.servicebase.handler.MyException;
import com.pilipili.zeus.eduService.entity.EduTeacher;
import com.pilipili.zeus.eduService.entity.excel.TeacherExcelData;
import com.pilipili.zeus.eduService.service.EduTeacherService;

/**
 * @author Unlimited_Blade_Works
 */

public class TeacherListener extends AnalysisEventListener<TeacherExcelData> {
    public EduTeacherService teacherService;

    public TeacherListener() {
    }

    public TeacherListener(EduTeacherService teacherService) {
        this.teacherService = teacherService;
    }

    @Override
    public void invoke(TeacherExcelData teacherExcelData, AnalysisContext analysisContext) {
        if (teacherExcelData == null) {
            throw new MyException(20001, "文件数据为空");
        }
        EduTeacher eduTeacher = this.isExistTeacher(teacherService, teacherExcelData.getName());
        if (eduTeacher == null) {
            eduTeacher = new EduTeacher();
            eduTeacher.setName(teacherExcelData.getName());
            eduTeacher.setIntro(teacherExcelData.getIntro());
            eduTeacher.setAvatar(teacherExcelData.getAvatar());
            eduTeacher.setSort(teacherExcelData.getSort());
            eduTeacher.setSort(teacherExcelData.getSort());
            teacherService.save(eduTeacher);
        }
    }
    private EduTeacher isExistTeacher(EduTeacherService teacherService, String name){
        QueryWrapper<EduTeacher> wrapper=new QueryWrapper<>();
        wrapper.eq("name",name);
        return teacherService.getOne(wrapper);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }
}
